package com.hzit.sb2.mapper;

import com.hzit.sb2.bean.Dept;
import com.hzit.sb2.bean.Emp;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

public interface EmpMapper {

    @Select({"<script> " +
            "select * from emp " +
            "where  1=1 " +
            "<if test='deptno!=null and deptno!=-1'> and deptno = #{deptno}</if> " +
            "<if test='ename!=null'> and ename like concat('%',#{ename},'%') </if> " +
            "</script>"})
    @Results({
            @Result(id = true,column = "deptno" , property = "deptno"),
            @Result(column = "dname" , property = "dname"),
            @Result(column = "loc" , property = "loc"),
            @Result(
                    property = "dept",//要封装的属性的名称
                    column = "deptno",//根据哪个字段查询
                    javaType = Dept.class,//要封装的实体类
                    //select代表查询哪个接口的方法获得数据、、调用UserMapper.findById(uid)获取数据
                    one = @One(select = "com.hzit.sb2.mapper.DeptMapper.findDeptById")
            )
    })
    List<Emp> findAll(@Param("deptno")Integer deptno, @Param("ename")String ename);

    @Insert("INSERT INTO emp(ename,job,mgr,hiredate,sal,deptno) VALUES (#{ename},#{job},#{mgr},#{hiredate},#{sal},#{deptno})")
    int insert(Emp emp);

    @Delete("delete from emp where empno=#{empno}")
    int deleteByPrimaryKey(@Param("empno") Integer empno);

    @Select("select * from emp where empno=#{empno}")
    @Results({
            @Result(id = true,column = "deptno" , property = "deptno"),
            @Result(column = "dname" , property = "dname"),
            @Result(column = "loc" , property = "loc"),
            @Result(
                    property = "dept",//要封装的属性的名称
                    column = "deptno",//根据哪个字段查询
                    javaType = Dept.class,//要封装的实体类
                    //select代表查询哪个接口的方法获得数据、、调用UserMapper.findById(uid)获取数据
                    one = @One(select = "com.hzit.sb2.mapper.DeptMapper.findDeptById")
            )
    })
    Emp selectByPrimaryKey(@Param("empno")Integer empno);

    @Update({"<script> " +
            "update emp set " +
            "<if test='ename!=null'> ename = #{ename} ,</if> " +
            "<if test='job!=null'> job = #{job} ,</if> " +
            "<if test='mgr!=null'> mgr = #{mgr} ,</if> " +
            "<if test='hiredate!=null'> hiredate = #{hiredate} ,</if> " +
            "<if test='sal!=null'> sal = #{sal} ,</if> " +
            "<if test='deptno!=null'> deptno = #{deptno} </if> " +
            "<where> empno = #{empno}</where> " +
            "</script>"})
    int update(Emp emp);
}
